package com.uidp.ui;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.wu.bean.MBGeneralCriteriaSearch;
import com.acsior.wu.bean.utils.IManagedProps;
import com.acsior.wu.nom.UIListManager;
import com.acsior.wu.request.RequestManager;
import com.uidp.domain.Usergroups;
import com.uidp.domain.Users;
import com.uidp.search.UserSearch;
import com.uidp.uitils.ManagedBeans;
import com.uidp.uitils.NavOutcomes;

public class MBSearchUsers extends MBGeneralCriteriaSearch<UserSearch, Users> {

	private List<Usergroups> allGropus = new ArrayList<Usergroups>();

	private String username;
	private String firstName;
	private String egn;
	private String family;
	private String selectedGroup;

	@SuppressWarnings("static-access")
	public MBSearchUsers() {
		super(UserSearch.class);
		try {
			this.allGropus = new Usergroups().searchActiveByUserName("");
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("static-access")
	@Override
	protected void specificSearch() throws SearchException, BusinessException {
		setTableRows(Users.searchAllActiveByUserName(getUsername(),
				getFirstName(), getFamily(), getEgn(), new Usergroups()
						.searchActiveByUserName(getSelectedGroup()).get(0)));

	}

	public String navToEdit() {
		Users s = getTableRows().get(getSelectedRow());
		RequestManager.putInRequest(ManagedBeans.regUser, s);
		return navigate(NavOutcomes.regUser);
	}

	/**
	 * @see com.acsior.wu.bean.MBGeneral#getManagedProp()
	 */
	protected IManagedProps getManagedProp() {
		return ManagedBeans.searchUsers;
	}

	/**
	 * Списък с всички роли
	 * 
	 * @return List
	 */
	public List<SelectItem> getNomGroups() {

		List<Usergroups> r = getAllGropus();
		List<String> groupNames = new ArrayList<String>();
		for (Usergroups s : getAllGropus()) {
			groupNames.add(s.getGroupname());
		}
		if (r != null && !r.isEmpty()) {
			return UIListManager.buildSelectItemsFromStrings(groupNames);
		}

		return new ArrayList<SelectItem>();
	}

	public List<Usergroups> getAllGropus() {
		return allGropus;
	}

	public void setAllGropus(List<Usergroups> allGropus) {
		this.allGropus = allGropus;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getEgn() {
		return egn;
	}

	public void setEgn(String egn) {
		this.egn = egn;
	}

	public String getFamily() {
		return family;
	}

	public void setFamily(String family) {
		this.family = family;
	}

	public String getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(String selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

}
